package com.example.jixeishi1.controller;


import com.example.jixeishi1.dao.HotaiDao;
import com.example.jixeishi1.entity.*;
import com.example.jixeishi1.service.HotaiService;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@SessionAttributes()
public class UsersController {
    @Autowired
    HotaiService hotaiService;
    @Autowired
    HotaiDao hotaiDao;


    private File myFile;  //上传的文件
    private String myFileContentType;  //文件类型
    private String myFileFileName;  //文件名
    private String destPath;


    //用户分页
    @RequestMapping("/yonghu/{p}")
    public String getyhfy(@PathVariable("p") int p, ModelMap map){
        System.out.println("用户");
        //System.out.println(p);
        PageInfo info1=hotaiService.getpage(p);
        List list=hotaiDao.getuser();
        System.out.println(list);
        map.put("list",list);
        map.put("info",info1);

        return "frame_a";
    }

    //按id查询显示
    @RequestMapping("/show")
    @ResponseBody
    public JsonResult1 ref(@RequestBody Users u){
        System.out.println(u.getId());
        List list1=hotaiDao.getyhid(u.getId());
        JsonResult1 json=new JsonResult1(200,"成为成本",list1);
        System.out.println(list1);
        //map.put("list1",list1);
        return json;
    }
    //按id查询所有赋值到文本框里
    @RequestMapping("/yhidcx")
    @ResponseBody
    public JsonResult1 refw(@RequestBody Users u){
        System.out.println("用户id查询");
        System.out.println(u.getId());
        List str=hotaiDao.getyhid(u.getId());
        JsonResult1 json=new JsonResult1(200,"",str);
        System.out.println(str);
        return json;
    }
    //封号管理
    @RequestMapping("/yhfhguanli")
    @ResponseBody
    public String refg4f(@RequestBody Users u){
        Users users=new Users();
        users.setId(u.getId());
        users.setConferred(1);
        int ok=hotaiDao.getyhfh(users);
        System.out.println(u.getId());
        if (ok>0){
            System.out.println("封号成功！");
            return "1";
        }else{
            System.out.println("封号失败！");
            return "0";
        }
    }
    //解封管理
    @RequestMapping("/yhfhguanlikai")
    @ResponseBody
    public String refgeef(@RequestBody Users u){
        Users users=new Users();
        users.setId(u.getId());
        users.setConferred(0);
        int ok=hotaiDao.getyhfh(users);
        System.out.println(u.getId());
        if (ok>0){
            System.out.println("解封成功！");
            return "1";
        }else{
            System.out.println("解封失败！");
            return "0";
        }
    }
    @RequestMapping("/yhzhcx")
    @ResponseBody
    public JsonResult1 reffw(@RequestBody Users u){
        System.out.println("用户账号查询");
        System.out.println(u.getUsername());
        List yhcx=hotaiDao.getyhname(u.getUsername());
        JsonResult1 json=new JsonResult1(200,"二位和第五百",yhcx);
        System.out.println(yhcx);
        return json;
    }
    //用户修改
    @RequestMapping("/yhxiugai")
    @ResponseBody
    public String yhupdate(@RequestBody Users u){
        System.out.println("用户修改");
        Users users=new Users();
        System.out.println(u.getYhusername());
        users.setYhusername(u.getYhusername());
        System.out.println(u.getUsername());
        users.setUsername(u.getUsername());
        users.setPassword(u.getPassword());
        users.setRole(u.getRole());
        System.out.println(u.getRole());
        users.setConferred(u.getConferred());
        System.out.println(u.getConferred());
        users.setId(u.getId());
        System.out.println(u.getId());
        System.out.println(users);
        int ok=hotaiDao.getyhupdate(users);
        if (ok>0){
            System.out.println("修改成功");
            return "1";
        }else{
            System.out.println("修改失败");
            return "0";
        }
    }
    //用户删除
    @RequestMapping("/yhsc")
    @ResponseBody
    public String yhsc(@RequestBody Users  u){
        System.out.println("用户删除");
        System.out.println(u.getId());
        int ok=hotaiDao.getyhsc(u.getId());
        System.out.println(ok);
        if (ok>0){
            System.out.println("删除成功");
            return "1";
        }else{
            System.out.println("删除失败");
            return "0";

        }
    }
    //商品分页
    @RequestMapping("/shangpin/{p}")
    public String getspfy(@PathVariable("p")int p, ModelMap map){
        System.out.println("商品");
        PageInfo info=hotaiService.getproduct(p);
        //List list=hotaiDao.getproduct();
        List list=hotaiDao.getflcx();
        map.put("info",info);
        map.put("list",list);
        return "frame_c";
    }
    @RequestMapping("/spflcx")
    @ResponseBody
    public JsonResult1 reffgerg(@RequestBody Product p, ModelMap map){
        System.out.println("商品分类查询");
        System.out.println(p.getFlname());
        List spflcx=hotaiDao.getspflcx(p.getFlname());
        JsonResult1 json=new JsonResult1(200,"商品分类查询",spflcx);
        System.out.println(spflcx);
        return json;
    }

    @RequestMapping("/spmccx")
    @ResponseBody
    public JsonResult1 reffgeferg(@RequestBody Product p,ModelMap map){
        System.out.println(p.getName());
        List spflcx=hotaiDao.getspmccx(p.getName());
        JsonResult1 json=new JsonResult1(200,"商品分类查询",spflcx);
        System.out.println(spflcx);
        return json;
    }

    @RequestMapping("/spadd")
    @ResponseBody
    public String fileUpload(MultipartFile [] file1,Product p) throws IOException {
        System.out.println("商品添加---上传图片");
        String path ="W:\\HB\\boot1\\JiXeiShi1\\target\\classes\\static\\img\\JiXeiShu";
        System.out.println("项目真实路径：" + path + "\n" +p.getCategoryid()+"\n"+p.getName()+"\n"+p.getSubtitle()+"\n"+p.getDetail()+"\n"+p.getPrice()+"\n"+p.getStock()+"\n"+p.getStatus()+"\n"+p.getXinghaoname());
        System.out.println(file1);
        String name="";
        for (MultipartFile file : file1) {
            name = file.getOriginalFilename(); //原来的文件名
            File ff = new File(path, name);
            System.out.println(name);
            file.transferTo(ff);    //存盘
        }
        List list=new ArrayList();
        int  wew=p.getName().lastIndexOf(",");
        String spmc=p.getName().substring(wew+1);
        System.out.println(spmc);

        int  spbt1=p.getSubtitle().lastIndexOf(",");
        String spbt=p.getSubtitle().substring(spbt1+1);
        System.out.println(spbt);

        int  spxq1=p.getDetail().lastIndexOf(",");
        String spmcw=p.getDetail().substring(spxq1+1);
        System.out.println(spmcw);
        list.add(new Product(p.getCategoryid(),spmc,spbt,name,spmcw,p.getPrice(),p.getStock(),p.getStatus(),p.getXinghaoname(),new Date()));
        int ok=hotaiDao.getproductadd(list);
        System.out.println(list);
        if (ok>0){
            return "1";
        }else {
            return "0";
        }

    }
    //商品修改
    @RequestMapping("/spxiugai")
    @ResponseBody
    public String fileUploadefwef(MultipartFile [] file2,Product p, HttpServletResponse response) throws IOException {
        System.out.println("商品修改");
        response.setCharacterEncoding("utf-8");
        String path ="D:\\11 IDEA课程  Maven\\jixieshihotai\\target\\classes\\static\\img";
        System.out.println("项目真实路径：" + path + "\n");
        String name="";
        for (MultipartFile file : file2) {
            name = file.getOriginalFilename(); //原来的文件名
            File ff = new File(path, name);
            System.out.println(name);
            file.transferTo(ff);    //存盘
        }
        int  wew=p.getName().lastIndexOf(",");
        String spmc=p.getName().substring(wew+1);
        System.out.println(spmc);

        int  spbt1=p.getSubtitle().lastIndexOf(",");
        String spbt=p.getSubtitle().substring(spbt1+1);
        System.out.println(spbt);

        int  spxq1=p.getDetail().lastIndexOf(",");
        String spmcw=p.getDetail().substring(spxq1+1);
        System.out.println(name);
        Product pa=new Product();

        pa.setCategoryid(p.getCategoryid());
        System.out.println(p.getCategoryid());

        pa.setName(spmc);
        System.out.println(spmc);

        pa.setSubtitle(spbt);
        System.out.println(spbt);

        pa.setMainimage(name);
        System.out.println(name);

        pa.setDetail(spmcw);
        System.out.println(spmcw);

        pa.setPrice(p.getPrice());
        System.out.println(p.getPrice());

        pa.setStatus(p.getStatus());
        System.out.println(p.getStatus());

        pa.setStock(p.getStock());
        System.out.println(p.getStock());

        pa.setXinghaoname(p.getXinghaoname());
        System.out.println(p.getXinghaoname());

        pa.setId(p.getId());
        System.out.println(p.getId());

        int ok=hotaiDao.getspxiuigai(pa);
        if (ok>0){
            return "1";
        }else {
            return "0";
        }

    }

    //商品id查询
    @RequestMapping("/spidcx")
    @ResponseBody
    public JsonResult1 getspidcx(@RequestBody Product  p){
        System.out.println("商品id 查询");
        List  list=hotaiDao.getspidcx(p.getId());
        JsonResult1 json=new JsonResult1(200,"商品id查询",list);
        System.out.println(json);
        return json;
    }
    


    //商品删除
    @RequestMapping("/sc")
    @ResponseBody
    public String getspsc(@RequestBody Product  p){
        System.out.println("商品删除");
        int ok=hotaiDao.getproductsc(p.getId());
        if (ok>0){
            System.out.println("删除成功");
            return "1";
        }else{
            System.out.println("删除失败");
            return "0";

        }
    }


    @RequestMapping("/xingxi")
    public String getxxfy(){
        System.out.println("信息");

        return "frame_e";
    }



    @RequestMapping("/dingdan/{p}")
    public String getddfy(@PathVariable("p")int p,ModelMap map){
        System.out.println("订单");
        PageInfo info=hotaiService.getddfenye(p);
        map.put("info",info);
        return "frame_b";
    }
    //订单删除
    @RequestMapping("/ddsc")
    @ResponseBody
    public String ddsc(@RequestBody Order o){
        System.out.println("订单删除");
        System.out.println(o.getId());
        int ok=hotaiDao.getddsc(o.getId());
        System.out.println(ok);
        if (ok>0){
            System.out.println("删除成功");
            return "1";
        }else{
            System.out.println("删除失败");
            return "0";

        }
    }
    @RequestMapping("/ddztcx")
    @ResponseBody
    public JsonResult1 reefeffw(@RequestBody Order o){
        System.out.println("订单状态查询");
        System.out.println(o.getTaostatus());
        List yhcx=hotaiDao.getddztcx(o.getTaostatus());
        JsonResult1 json=new JsonResult1(200,"二位和第五百",yhcx);
        System.out.println(yhcx);
        return json;
    }
    @RequestMapping("/huiyuan")
    public String gethyfy(){
        System.out.println("会员");

        return "frame_d";
    }
}
